const app = getApp()

import Dialog from '@vant/weapp/dialog/dialog';
import {
    showMsg
} from '../../../../utils/config.js'

import {
    reqhiddenDangerupdate,
} from '../../../../request/api.js'
import {
    validateEmoji
} from '../../../../utils/validate.js'

Page({
    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        info: {}, //告警信息
        option: [{
                text: '现场解决',
                value: '现场解决'
            },
            {
                text: '转化工单',
                value: '转化工单'
            },
        ],
        confirmResult: "现场解决",
        optionmode: [{
                text: '现场解决',
                value: '现场解决'
            },
            {
                text: '转化工单',
                value: '转化工单'
            },
        ],
        confirmmode: "现场解决",
        remark: "",
        maintenanceId: "",
        maintenanceName: "",
        modeFlag: false, //控制处理方式
        maintenanceFlag: false, //控制维保单位
    },

    onLoad(e) {
        this.setData({
            info: JSON.parse(e.info),
        })
        console.log(JSON.parse(e.info));
        wx.removeStorageSync('maintenanceId')
        wx.removeStorageSync('maintenanceName')
        if (JSON.parse(e.info).source == 0) {
            // 设备报警隐患
            let option = [{
                    text: '真实隐患',
                    value: '真实隐患'
                },
                {
                    text: '误报',
                    value: '误报'
                },
                {
                    text: '测试',
                    value: '测试'
                },
            ]
            this.setData({
                option,
                confirmResult: "真实隐患",
                modeFlag: true,
            })
        }
    },
    onShow() {
        if (wx.getStorageSync('maintenanceId')) {
            console.log(wx.getStorageSync('maintenanceId'));
            this.setData({
                maintenanceId: wx.getStorageSync('maintenanceId'),
                maintenanceName: wx.getStorageSync('maintenanceName'),
            })
        }
    },
    // onUnload() {
    //     wx.removeStorageSync('checkboxList')
    // },
    bindinput(e) {
        let remark = validateEmoji(e.detail.value.replace(/\s+/g, ''))
        if (remark.length > 100) {
            this.setData({
                remark: remark.substring(0, 100), //截取前200位
            })
            return;
        }
        this.setData({
            remark
        })
    },
    onDropChange(e) {
        console.log(e);
        this.setData({
            confirmResult: e.detail
        })
        if (this.data.info.source == 1) {
            if (e.detail == '转化工单') {
                this.setData({
                    maintenanceFlag: true
                })
            } else {
                this.setData({
                    maintenanceFlag: false
                })
            }
        } else {
            if (e.detail == '测试') {
                this.setData({
                    modeFlag: false,
                    maintenanceFlag: false,
                    confirmmode: "现场解决"
                })
            } else {
                this.setData({
                    modeFlag: true
                })
            }
        }
    },
    onDropChangemode(e) {
        console.log(e);
        this.setData({
            confirmmode: e.detail
        })
        if (e.detail == '转化工单') {
            this.setData({
                maintenanceFlag: true
            })
        } else {
            this.setData({
                maintenanceFlag: false
            })
        }
    },
    // 选择维保单位
    onChoseDevices() {
        let info = JSON.stringify(this.data.info)
        console.log(info);
        wx.navigateTo({
            url: `/pages/message-index-page/hidden-maintenance/hidden-maintenance?info=${info}`,
        })
    },
    async onSave() {
        if (this.data.info.source == 1 && this.data.confirmResult == '转化工单' && this.data.maintenanceId == '') return showMsg('请选择维保单位')
        if (this.data.info.source == 0 && this.data.confirmmode == '转化工单' && this.data.maintenanceId == '') return showMsg('请选择维保单位')
        let mode;
        if (this.data.info.source == 1) {
            // 人为报警
            if (this.data.confirmResult == '转化工单') {
                mode = 1
            } else {
                mode = 0
            }
        } else {
            // 设备上报
            if (this.data.confirmResult == '测试') {
                mode = '';
            } else {
                if (this.data.confirmmode == '转化工单') {
                    mode = 1
                } else {
                    mode = 0
                }
            }
        }
        let res = await reqhiddenDangerupdate({
            id: this.data.info.id,
            userId: app.globalData.userId,
            username: app.globalData.username,
            confirmResult: this.data.confirmResult,
            mode: mode,
            maintenanceId: this.data.maintenanceId,
            remark: this.data.remark,
        })
        if (res.code !== 20000) return showMsg(res.message)
        showMsg("处理成功", "success")
        setTimeout(() => {
            wx.navigateBack({
                delta: 1,
            })
        }, 1000)
    },
})